<template>
	<div>
        <el-dialog title="新增" :close-on-click-modal="false" :visible.sync="visible">
            <el-form :model="dataForm" :rules="dataRule" labelWidth="120px" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                     label-width="80px" style="height: 420px;overflow-y: hidden">
                <el-form-item label="评估年度" prop="evalYear">
                    <el-select v-model="dataForm.evalYear" placeholder="请选择评估年度">
                        <el-option :label="year.label" :value="year.value" v-for="(year,idx) of yearsList" :key="idx"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注" prop="biz">
                    <el-input v-model="dataForm.biz" :maxlength="1000"></el-input>
                </el-form-item>
                <el-form-item label="评估区域" prop="reginId">
                    <el-input v-model="dataForm.reginName" :disabled="onlyOneArea" v-if="onlyOneArea"></el-input>
                    <el-select v-model="dataForm.reginId" placeholder="请选择评估区域" v-else>
                        <el-option :label="area.name" :value="area.code" v-for="(area,idx) of areasList" :key="idx"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="填报人账号" prop="createBy">
                    <el-input v-model="dataForm.createBy" disabled></el-input>
                </el-form-item>
                <el-form-item label="填报人" prop="auditByName">
                    <el-input v-model="dataForm.createByName" disabled></el-input>
                </el-form-item>
                <el-form-item label="填报时间" prop="lstUpdTime">
                    <el-input v-model="dataForm.lstUpdTime" disabled></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
			</span>
        </el-dialog>
	</div>
</template>

<script>
	import {
		Debounce
	} from '@/utils/debounce'
	export default {
		data() {
			return {
				visible: false,
                onlyOneArea: false,//有几个区县的权限？ 1个的话不必选择
				dataForm: {
                    evalMainId: null,
                    biz: null,
                    reginId: "xj",
                    reginName: "",
                    evalYear: "",
                    lstUpdTime: wfy.format('yyyy-MM-dd HH:mm:ss',new Date()),
                    estimateYear: "",
                    address: "",
                    createByName: this.$store.state.user.name,
                    createBy: this.$store.state.user.name
				},
				dataRule: {
                    evalYear: [
                        { required: true, message: '请选择评估年度', trigger: 'change' },
                    ],
                    reginId: [
                        { required: true, message: '请选择评估区域', trigger: 'change' },
                    ]
                },
				yearsList: [],
                areasList: [
                    {
                        code: "xj",
                        name: "新疆"
                    }
                ]
			}
		},
		methods: {
			init(id) {
                this.yearsList = [];
              	for (let i = 2024; i < 2060; i++) {
                  	this.yearsList.push({
						value : i,
					  	label : i
					})
              	}
			  	this.dataForm.evalYear = new Date().getFullYear();
                //   let areaList = this.$store.state.user.area.area;
                // if(areaList.length > 1){
                //     this.areasList = areaList;
                //     this.dataForm.reginId = areaList[0].code;
                //     this.onlyOneArea = false;
                // }else{
                //     this.dataForm.reginId = areaList[0].code;
                //     this.dataForm.reginName = areaList[0].name;
                //     this.onlyOneArea = true;
                // }
				this.visible = true
				this.$nextTick(() => {
					this.$refs['dataForm'].resetFields()
					if (this.dataForm.evalMainId) {

					}
				})
			},
            // 查询是否存在当前年度+地区的自评信息
            getOldTargetList(){
                return new Promise((resolve) => {
                    this.$http({
                        url: this.$http.adornUrl('/eval/getNowEvalInfo'),
                        method: 'post',
                        data: this.$http.adornData({
                            "evalYear": this.dataForm.evalYear,
                            "reginId": this.dataForm.reginId,
                        })
                    }).then(({
                        data
                    }) => {
                        resolve(data);
                    })
                })
            },
			// 表单提交
      dataFormSubmit: Debounce(function() {
                console.log('🚀 ~ this.dataForm', this.dataForm)
				this.$refs['dataForm'].validate(async (valid) => {
					if (valid) {
                        let orReadyResult = await this.getOldTargetList();
                        console.log('🚀 ~ orReadyResult', orReadyResult)

                        if(orReadyResult.length){
                            this.visible = false;
                            // this.$emit('initSuccess',orReadyItem)
                            console.log('🚀 ~ orReadyResult[0].evaluationMain', orReadyResult[0].evaluationMain)
                            if(orReadyResult[0].evaluationMain.evalStatus === "O" || orReadyResult[0].evaluationMain.evalStatus === "N"){
                                let orReadyItem = {
                                    affixList: [],
                                    anIndexEvaluationMain: orReadyResult[0].evaluationMain,
                                    currentEvalDtl: orReadyResult[0].currentEvalDtl,
                                    currentIndexInfo: orReadyResult[0].currentIndexInfo,
                                    scoreList: orReadyResult[0].scoreList
                                }
                                this.$emit('initSuccess',orReadyItem)
                            }else{
                                let reginItem = this.areasList.find((area) => {
                                    return area.code === this.dataForm.reginId
                                })
                                this.$alert("【"+ this.dataForm.evalYear + "】年度【" + reginItem.name + "】已经存在填报数据，并且数据状态不可编辑！请切换填报条件后重新尝试","友情提醒",{
                                    callback: () => {
                                        this.$emit('initFail')
                                    }
                                })
                            }

                        }else{
                            this.$http({
                            	url: this.$http.adornUrl('/eval/initNowEvalInfo'),
                            	method: "post",
                            	data: this.$http.adornData({
                                    ...this.dataForm,
                                    evalMainId : wfy.uuid(20)
                                })
                            }).then(({
                            	data
                            }) => {
                                this.visible = false

                                if(wfy.empty(data.anIndexEvaluationMain)){
                                    this.$alert(data.reMsg,"友情提醒",{
                                        callback: () => {

                                        }
                                    })
                                }else{
                                    this.$emit('initSuccess',data)
                                }
                            })
                        }
					}
				})
			})
		}
	}
</script>
